﻿#region Using statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RhythmComet.Physics;
using FMOD;
using RhythmComet.Services;
using RhythmComet.Particles;
using System.Collections.Generic;
#if !XBOX
using System;
using System.Threading;
#endif
#endregion

namespace RhythmComet
{
    public interface IRetryMenuState : IGameState { }

    public sealed class RetryMenuState : GameState, IRetryMenuState
    {
        #region Fields and properties
        Texture2D background;
        Vector2 backgroundPosition;
#if !XBOX
        string[] menuOptions = new string[3] { "Retry", "Choose another song", "Exit" };
#else
        string[] menuOptions = new string[2] { "Retry", "Exit" };
#endif
        SpriteFont font;
        SpriteFont font2;
        int optionSelected;
#if !XBOX
        bool otherSong = false;
#else
        bool otherSong = true;
#endif
        #endregion

        #region Initializing
        public RetryMenuState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IRetryMenuState), this);
            optionSelected = 0;
        }

        protected override void LoadContent()
        {
            background = game.Content.Load<Texture2D>("sprites/background");
            font = game.Content.Load<SpriteFont>(@"fonts\GridExercise");
            backgroundPosition = new Vector2((game.GraphicsDevice.Viewport.Width / 2 - background.Width / 2),
                (game.GraphicsDevice.Viewport.Height / 2 - background.Height / 2));
            font2 = game.Content.Load<SpriteFont>(@"fonts\BerlinSmallCaps");
        }
        #endregion

        #region Update and draw
        public override void Update(GameTime gameTime)
        {
            if (Input.GamePadState.Buttons.Back == ButtonState.Pressed)
                game.Exit();
#if !XBOX
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A) || Input.WasPressed(Keys.Enter))
#else
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Buttons.A))
#endif
            {
                if (optionSelected == 0)
                {
                    game.Comet.Reset();
                    game.BeatManager.Reset();
                    game.Score.Reset();
                    game.ObstacleManager.Reset();
#if !XBOX
                    game.Player.Stop();
                    game.Player.Initialize();
#endif
                    GameManager.PopState();
                }
#if !XBOX
                else if (optionSelected == 1)
                {
                    Thread box = new Thread(new ThreadStart(OpenSong));
                    box.SetApartmentState(ApartmentState.STA);
                    box.Start();
                }
#endif
                else
                {
                    Game.Exit();
                }
            }
#if !XBOX
            if (Input.WasPressed(Keys.Down) || Input.WasPressed(Buttons.DPadDown) || Input.WasPressed(Buttons.LeftThumbstickDown))
#else
            if (Input.WasPressed(Buttons.DPadDown) || Input.WasPressed(Buttons.LeftThumbstickDown))
#endif
                optionSelected = (optionSelected + 1) % 4;
#if !XBOX
            if (Input.WasPressed(Keys.Up) || Input.WasPressed(Buttons.DPadUp) || Input.WasPressed(Buttons.LeftThumbstickUp))
#else
            if (Input.WasPressed(Buttons.DPadUp) || Input.WasPressed(Buttons.LeftThumbstickUp))
#endif
                optionSelected = optionSelected - 1;

            if (optionSelected < 0)
                optionSelected = 3;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Color color;
            Vector2 position = new Vector2(100, 80);

            game.GraphicsDevice.Clear(Color.Black);

            game.SpriteBatch.Begin();

            game.SpriteBatch.Draw(background, backgroundPosition, Color.White);

            if (otherSong)
            {
                menuOptions[0] = "Play Game";
#if !XBOX
                DrawID3info();
#endif
            }

            for (int i = 0; i < menuOptions.Length; i++)
            {
                if (optionSelected == i)
                    color = Color.Red;
                else
                    color = Color.White;

                game.SpriteBatch.DrawString(font, menuOptions[i], position, color);
                position.Y += 20;
            }

            game.SpriteBatch.End();

            base.Draw(gameTime);
        }
#if !XBOX
        public void DrawID3info()
        {
            Vector2 infoPosition = new Vector2(game.SpriteBatch.GraphicsDevice.Viewport.Width / 2 - 100, game.SpriteBatch.GraphicsDevice.Viewport.Height / 2 + 100);
            game.SpriteBatch.DrawString(font2, "Artist:", infoPosition, Color.White);
            game.SpriteBatch.DrawString(font2, game.Player.Artist, new Vector2(infoPosition.X + 100, infoPosition.Y), Color.White);
            infoPosition.Y += 20;
            game.SpriteBatch.DrawString(font2, "Title:", infoPosition, Color.White);
            game.SpriteBatch.DrawString(font2, game.Player.Title, new Vector2(infoPosition.X + 100, infoPosition.Y), Color.White);
            infoPosition.Y += 20;
            game.SpriteBatch.DrawString(font2, "Album:", infoPosition, Color.White);
            game.SpriteBatch.DrawString(font2, game.Player.Album, new Vector2(infoPosition.X + 100, infoPosition.Y), Color.White);
            infoPosition.Y += 20;
        }
        

        
        void OpenSong()
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();

            fileDialog.Title = "Choose your song";

            fileDialog.Filter = "Music Files (*.mp3)|*.mp3";

            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                game.Player.SongFilename = fileDialog.FileName;
                game.Player.Initialize();
                game.Player.ReadTags();

                otherSong = true;
                optionSelected = 0;
            }
        }
#endif
        #endregion
    }
}
